//
//  InfoShareCellTableViewCell.m
//  Utils
//
//  Created by enghou on 2018/6/4.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "InfoShareCellTableViewCell.h"
#import "NetWorkerManager.h"
@interface InfoShareCellTableViewCell()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *unitLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLbl;

@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *images;

@end
@implementation InfoShareCellTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    UITapGestureRecognizer *firstTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(firstAction:)];
    UITapGestureRecognizer *secondTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(secondAction:)];
    UITapGestureRecognizer *thirdTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(thirdAction:)];
    NSArray *gestures = @[firstTap,secondTap,thirdTap];
    [self.images enumerateObjectsUsingBlock:^(UIImageView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj addGestureRecognizer:gestures[idx]];
        obj.hidden = YES;
    }];
}

- (IBAction)firstAction:(id)sender {
    [self showContentWithIndex:0];
}

- (IBAction)secondAction:(id)sender {
    [self showContentWithIndex:1];
}

- (IBAction)thirdAction:(id)sender {
    [self showContentWithIndex:2];
}

-(void)showContentWithIndex:(NSInteger)index{
    if (index < self.model.files.count) {
        FileItemModel *model = [self.model.files objectAtIndex:index];
        switch (model.type) {
            case Image:
                if ([self.theDelegate respondsToSelector:@selector(showImageWithImage:currentIndex:sourceView:)]) {
                    NSMutableArray *images = [NSMutableArray array];
                    for (UIImageView *imageView in self.images) {
                        if (imageView.image) {
                            [images addObject:imageView.image];
                        }
                    }
                    [self.theDelegate showImageWithImage:images currentIndex:index sourceView:self.images[index]];
                }
                break;
            case Audio:
            {
                if ([self.theDelegate respondsToSelector:@selector(showAudioWithURL:)]) {
                    NSString *absoluteString = [NSString stringWithFormat:@"%@/%@",ServerPath,model.path];
                    [self.theDelegate showAudioWithURL:[NSURL URLWithString:absoluteString]];
                }
            }
                break;
            case Video:
            {
                if ([self.theDelegate respondsToSelector:@selector(showVideoWithURL:)]) {
                    NSString *absoluteString = [NSString stringWithFormat:@"%@/%@",ServerPath,model.path];
                    [self.theDelegate showVideoWithURL:[NSURL URLWithString:absoluteString]];
                }
            }
                break;
            case Document:
            {
                if ([self.theDelegate respondsToSelector:@selector(showFileWithURL:)]) {
                    NSString *absoluteString = [NSString stringWithFormat:@"%@/%@",ServerPath,model.path];
                    [self.theDelegate showFileWithURL:[NSURL URLWithString:absoluteString]];
                }
            }
                break;
            default:
                break;
        }
    }
}

-(void)setModel:(InfoItemModel *)model{
    _model = model;
    self.nameLabel.text = _model.name;
    self.unitLabel.text = _model.unit;
    self.timeLbl.text = _model.time;
    if (_model.files.count > 0) {
        [self.images enumerateObjectsUsingBlock:^(UIImageView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.hidden = YES;
        }];
        [_model.files enumerateObjectsUsingBlock:^(FileItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIImageView *image = self->_images[idx];
            image.hidden = NO;
            switch (obj.type) {
                case Image:
                {
                    [image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ServerPath,obj.path]]];
                }
                    break;
                case Video:
                {
                    [image setImage:[UIImage imageNamed:@"icon_video"]];
                }
                    break;
                case Audio:
                {
                    [image setImage:[UIImage imageNamed:@"icon_audio"]];
                }
                    break;
                case Document:
                {
                    [image setImage:[UIImage imageNamed:@"icon_document"]];
                }
                    break;
                default:
                    break;
            }
        }];
    }else{
        [self.images enumerateObjectsUsingBlock:^(UIImageView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.hidden = YES;
        }];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
